package com.chocus.core.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import com.chocus.common.web.Constants;
import com.chocus.core.service.upload.UploadService;

import net.fckeditor.response.UploadResponse;

@Controller
@RequestMapping("/upload/")
public class UploadController {

	@Autowired
	private UploadService uploadService;

	/**
	 * 单文件上传
	 * 
	 * @param pic
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("uploadPic.do")
	public void uploadPic(@RequestParam(required = false) MultipartFile pic, HttpServletResponse response)
			throws Exception {
		System.out.println(pic.getOriginalFilename());
		String path = uploadService.uploadPic(pic.getBytes(), pic.getOriginalFilename(), pic.getSize());
		String url = Constants.img_url + path;
		JSONObject jb = new JSONObject();
		jb.put("url", url);
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().write(jb.toString());
	}

	/**
	 * 多文件上传
	 * 
	 * @param pics
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("uploadPics.do")
	public @ResponseBody List<String> uploadPics(@RequestParam(required = false) MultipartFile[] pics,
			HttpServletResponse response) throws Exception {
		List<String> urls = new ArrayList<String>();
		for (MultipartFile pic : pics) {
			String path = uploadService.uploadPic(pic.getBytes(), pic.getOriginalFilename(), pic.getSize());
			String url = Constants.img_url + path;
			urls.add(url);
		}
		return urls;
	}
	
	/**
	 * 多文件上传
	 * 
	 * @param pics
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("uploadFCK.do")
	public void uploadFCK(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//因为无法知道上传文件的名字，所以将request转为MultipartRequest
		MultipartRequest mr = (MultipartRequest) request;
		Map<String, MultipartFile> fileMap = mr.getFileMap();
		Set<Entry<String, MultipartFile>> entrySet = fileMap.entrySet();
		for (Entry<String, MultipartFile> entry : entrySet) {
			MultipartFile pic = entry.getValue();
			String path = uploadService.uploadPic(pic.getBytes(), pic.getOriginalFilename(), pic.getSize());
			String url = Constants.img_url + path;
//			//返回全路径给FCK java-core-2.6.jar
//			UploadResponse ok = UploadResponse.getOK(url);
//			response.setContentType("application/json;charset=utf-8");
//			response.getWriter().print(ok);
			JSONObject jb = new JSONObject();
			jb.put("url", url);
			jb.put("error", 0);
			response.setContentType("application/json;charset=utf-8");
			response.getWriter().write(jb.toString());
		}
	}
}
